/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Fabricas.Factory;
import Interfaces.IJuego;
import java.awt.Dimension;
import javax.swing.JOptionPane;

/**
 *
 * @author nacho
 */
public class JIFAltaCategoria extends javax.swing.JInternalFrame {
    
    private Factory f = null;
    private IJuego ij = null;
    
    public JIFAltaCategoria(Factory f) throws Exception{
        
        this.f = f;
        this.ij = f.getIJuego();
        initComponents();

        this.jBtnAceptarCat.setEnabled(false);
    }
    
    private void centrarVentana(){
        
        Dimension desktopSize = JIFInicial.getDPInicioDimension();
        Dimension jInternalFrameSize = this.getSize();
        this.setLocation((desktopSize.width - jInternalFrameSize.width)/2,
                                   (desktopSize.height- jInternalFrameSize.height)/2);
        //setLocation(55, 55);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLblNombreCat = new javax.swing.JLabel();
        jTFNomCategoria = new javax.swing.JTextField();
        jBtnAceptarCat = new javax.swing.JButton();
        jBtnCancelarCat = new javax.swing.JButton();

        setClosable(true);
        setToolTipText("");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jLblNombreCat.setText("Nombre de Categoria:");

        jTFNomCategoria.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTFNomCategoriaKeyReleased(evt);
            }
        });

        jBtnAceptarCat.setText("Aceptar");
        jBtnAceptarCat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAceptarCatActionPerformed(evt);
            }
        });

        jBtnCancelarCat.setText("Cancelar");
        jBtnCancelarCat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCancelarCatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jBtnAceptarCat)
                    .addComponent(jLblNombreCat))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTFNomCategoria)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 107, Short.MAX_VALUE)
                        .addComponent(jBtnCancelarCat)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLblNombreCat)
                    .addComponent(jTFNomCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnAceptarCat)
                    .addComponent(jBtnCancelarCat))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnCancelarCatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCancelarCatActionPerformed
        
        this.dispose();
        JIFInicial.BloquearJFInicial();
        
    }//GEN-LAST:event_jBtnCancelarCatActionPerformed

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing

        JIFInicial.BloquearJFInicial();
        
    }//GEN-LAST:event_formInternalFrameClosing

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        
        centrarVentana();
        
    }//GEN-LAST:event_formInternalFrameOpened

    private void jBtnAceptarCatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAceptarCatActionPerformed
        if(evt.getSource() == this.jBtnAceptarCat){
            
            Integer retorno = -1;
            String nombre_cat = this.jTFNomCategoria.getText();
            try{
                
                retorno = ij.ingresarCategoria(nombre_cat);
            
            }
            catch(Exception e){
                
                JOptionPane.showMessageDialog(this, e.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE, null);
                
            }
            if(retorno == 1){
                
                JOptionPane.showMessageDialog(null, "Categoria '"+nombre_cat+"' agregada con éxito", "Operación con éxito", JOptionPane.PLAIN_MESSAGE);
                this.dispose();
                JIFInicial.BloquearJFInicial();
                
            }
            else if(retorno == -1){
                
                JOptionPane.showMessageDialog(null, "Ya existe la categoria '"+nombre_cat+"'", "Error", JOptionPane.ERROR_MESSAGE);
                
            }            
            
            
        }
        
        
    }//GEN-LAST:event_jBtnAceptarCatActionPerformed

    private void jTFNomCategoriaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTFNomCategoriaKeyReleased
        if(this.jTFNomCategoria.getText().length() >0){
        
            this.jBtnAceptarCat.setEnabled(true);
        
        }
        
        else{
        
            this.jBtnAceptarCat.setEnabled(false);
        
        }
        
    }//GEN-LAST:event_jTFNomCategoriaKeyReleased

            
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnAceptarCat;
    private javax.swing.JButton jBtnCancelarCat;
    private javax.swing.JLabel jLblNombreCat;
    private javax.swing.JTextField jTFNomCategoria;
    // End of variables declaration//GEN-END:variables
}
